using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Globalization;

namespace Medianamik.UI.Controls
{
    public class MedianamikControlLabel : WebControl
    {
        public bool IsRequired
        {
            get;
            set;
        }

        public bool IsLanguageNeutral
        {
            get;
            set;
        }

        public string Text
        {
            get;
            set;
        }

        public string MedianamikToolTip
        {
            get;
            set;
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Label;
            }
        }

        public virtual bool IsInsideMedianamik
        {
            get { return Page as BasePage != null; }
        }

        public string ForControlId
        {
            get;
            set;
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);

            if (IsInsideMedianamik)
            {
                writer.AddAttribute("class", "EditionPage");
            }
            else
            {
                writer.AddAttribute("for", ForControlId);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            RenderBeginTag(writer);

            if (IsRequired && IsInsideMedianamik)
            {
                writer.Write(@"<img src=""/Medianamik/images/Icones/Ico_ProprieteObligatoire.png"" class=""IconeLabel"" alt="""" />&nbsp;&nbsp;");
            }

            writer.Write(Text);

            if (IsLanguageNeutral && IsInsideMedianamik)
            {
                writer.Write(@"&nbsp;<img src=""/Medianamik/images/Icones/Ico_ProprieteNeutreLangue.png"" class=""IconeLabel"" alt="""" />");
            }

            if (!IsInsideMedianamik)
            {
                if (IsRequired)
                {
                    writer.Write(@"&nbsp;<span class=""Required"">*</span>");
                }
                if(!String.IsNullOrEmpty(MedianamikToolTip))
                {
                    writer.Write(String.Format("<a class=\"Tooltip\" rel=\"{0}\">{1}</a>",
                        MedianamikToolTip, Translator.Translate("Medianamik_Controls", "Tooltip")));
                }
            }

            RenderEndTag(writer);
        }
    }
}